/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ufrgs.inf.prav.interop.jsf.components;

import br.ufrgs.inf.prav.interop.metadata.MetadataHandler;
import br.ufrgs.inf.prav.interop.resource.MetadataResourceFinder;
import br.ufrgs.inf.prav.interop.resource.ResourceFinder;
import java.io.IOException;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.component.UIOutput;
import javax.faces.component.UIViewRoot;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import obai.control.PlataformBean;
import obai.control.PlataformBean.Plataform;

/**
 *
 * @author Fernando Arena Varella
 * @version 1.0
 */
public abstract class PravBaseComponent extends UIOutput {

    public static final String COMPONENT_FAMILY = "PRAV_BASE";
    public static final String COMPONENT_TYPE = "PRAV_BASE";

    PlataformBean plataformBean = getPlataformBean();
    ResourceFinder resourceFinder = getResourceFinder();
    MetadataResourceFinder metadataResourceFinder = getMetadataResourceFinder();

    //core attributes
    protected String styleClass, id, style, title;
    //language attributes
    protected String dir, lang;
    //keyboard attributes
    protected String accesskey, tabindex;

    //specific attributes
    protected String value;

    //metadata attributes
    protected String lomId;

    private boolean displayMobile = true, displayTv = true, displayWeb = true;


    public PravBaseComponent(){

    }

    protected void addBaseAttributes(ResponseWriter writer) throws IOException{
        writer.writeAttribute("class", styleClass, "styleClass");
        writer.writeAttribute("id", id, "id");
        writer.writeAttribute("style", style, "style");
        writer.writeAttribute("title", title, "title");
        writer.writeAttribute("dir", dir, "dir");
        writer.writeAttribute("lang", lang, "lang");
        writer.writeAttribute("accesskey", accesskey, "accesskey");
        writer.writeAttribute("tabindex", tabindex, "tabindex");

        //specific attributes
//        writer.writeAttribute("mobileonly", mobileonly, "mobileonly");
//        writer.writeAttribute("tvonly", tvonly, "tvonly");
//        writer.writeAttribute("webonly", webonly, "webonly");
    }

    /**
     * Add css code to hide content that belongs only to a specific plataform
     * @param writer    the ResponseWriter which is handling the current component
     * @throws java.io.IOException
     */
    protected void hidePlataformSpecificContent(ResponseWriter writer) throws IOException {
        boolean mobile, tv, web;
//        mobile = Boolean.valueOf(mobileonly);
//        tv = Boolean.valueOf(tvonly);
//        web = Boolean.valueOf(webonly);
//
//        if (mobile || tv || web){
//            Plataform plataform = plataformBean.getPlataform();
//            String cssCode = "display: none;";
//            if (plataform == Plataform.MOBILE && !mobile)
//                writer.writeAttribute("style", cssCode, "style");
//            if (plataform == Plataform.DIGITALTV && !tv)
//                writer.writeAttribute("style", cssCode, "style");
//            if (plataform == Plataform.WEB && !web)
//                writer.writeAttribute("style", cssCode, "style");
//        }
    }

    protected boolean displayPlataform(){
        Plataform plataform = plataformBean.getPlataform();

        if (plataform == Plataform.MOBILE && !displayMobile)
            return false;
        if (plataform == Plataform.DIGITALTV && !displayTv)
            return false;
        if (plataform == Plataform.WEB && !displayWeb)
            return false;

        return true;
    }

    public void tmp(){
        UIViewRoot vr = FacesContext.getCurrentInstance().getViewRoot();
        List<UIComponent> children = vr.getChildren();
        for (UIComponent child : children) {
            try {
                HtmlOutputText htmlText = (HtmlOutputText) child;
                String text = (String)htmlText.getValue();
//                System.out.println(text);

                if (text.contains("</body>")){
                    System.out.println("achou!!");
                    text = text.replaceAll("</body>",
                            "<i:div displayWeb=\"false\">aqui estaria o menu da TV</i:div></body>");
                }
                htmlText.setValue(text);

            } catch (Exception e) {
//                System.out.println("Erro: " + e.getMessage());
            }
        }
        System.out.println("\n-\n");
    }

    /**
     * Hide all the components within the current component. It includes
     * static HTML and any other JSP tags.
     */
    protected void hideInnerComponents(){
        List<UIComponent> children = this.getChildren();
        for (UIComponent child : children) {
            child.setRendered(false);
        }
    }

    protected String makeReferenceFromMetadata(String itemID){
        getCurrentAppLocation();
        String platform;
        switch (plataformBean.getPlataform()){
            case DIGITALTV: platform = "DTV"; break;
            case MOBILE: platform = "Mobile"; break;
            default: platform = "WEB"; break;
        }
        System.out.println("chamando metadata");
        return metadataResourceFinder.makeFinalReference(lomId, platform);
//        return facade.getURLFromItemID(itemID, targetDevice);
    }

    /**
     * Rebuild the given reference to point to a plataform specific resource
     * (only if it exists).
     * Example: "img/image.png" will be renamed to "img/image_mobile.png" if
     * exists a "img/image_mobile.png"
     * @param src
     * @return
     */
    protected String makeFinalReference(String src){
        if (src == null)
            return null;
        return resourceFinder.makeFinalReference(src);
    }


    private String getCurrentAppLocation(){
        FacesContext ctx = FacesContext.getCurrentInstance();

        MetadataHandler.getAppConfig(ctx);

        return "null";
    }

    protected PlataformBean getPlataformBean(FacesContext context) {
        FacesContext fc = context.getCurrentInstance();
        PlataformBean plataformBean = (PlataformBean) fc.getApplication().
                createValueBinding("#{control$PlataformBean}").getValue(fc);
        return plataformBean;
    }
    protected ResourceFinder getResourceFinder(FacesContext context) {
        return new ResourceFinder();
    }
    protected PlataformBean getPlataformBean() {
        FacesContext fc = FacesContext.getCurrentInstance();
        PlataformBean plataformBean = (PlataformBean) fc.getApplication().
                createValueBinding("#{control$PlataformBean}").getValue(fc);
        return plataformBean;
    }
    protected ResourceFinder getResourceFinder() {
        return new ResourceFinder();
    }
    protected MetadataResourceFinder getMetadataResourceFinder() {
        return new MetadataResourceFinder();
    }

    /*@Override
    public void encodeBegin(FacesContext context) throws IOException {
		ResponseWriter writer =
			context.getResponseWriter();
		writer.startElement("img", this);
        //atts
        writer.writeAttribute("src", src, "src");
        writer.flush();

	}

    @Override
    public void encodeEnd(FacesContext context)
            throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        writer.endElement("img");
        writer.flush();
        return;
    }

    @Override
    public void decode(FacesContext context) {
    }*/

    // <editor-fold defaultstate="collapsed" desc="Getters And Setters">

    public String getAccesskey() {
        return accesskey;
    }

    public void setAccesskey(String accesskey) {
        this.accesskey = accesskey;
    }

    public String getStyleClass() {
        return styleClass;
    }

    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }


    public String getDir() {
        return dir;
    }

    public void setDir(String dir) {
        this.dir = dir;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLang() {
        return lang;
    }

    public void setLang(String lang) {
        this.lang = lang;
    }

    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public String getTabindex() {
        return tabindex;
    }

    public void setTabindex(String tabindex) {
        this.tabindex = tabindex;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    

    public boolean isDisplayMobile() {
        return displayMobile;
    }

    @Override
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setDisplayMobile(boolean displayMobile) {
        this.displayMobile = displayMobile;
    }

    public boolean isDisplayTv() {
        return displayTv;
    }

    public void setDisplayTv(boolean displayTv) {
        this.displayTv = displayTv;
    }

    public boolean isDisplayWeb() {
        return displayWeb;
    }

    public void setDisplayWeb(boolean displayWeb) {
        this.displayWeb = displayWeb;
    }

    public String getLomId() {
        return lomId;
    }

    public void setLomId(String lomId) {
        this.lomId = lomId;
    }

    



    // </editor-fold>


}
